var Member = function() {
    var datalist_url = $('.datalist_url').val();
    var view_url = $('.view_url').val();

    var _init = function() {
        var table = $('#data-table');

        table.dataTable({
            "processing": true,
            serverSide: true,// 开启服务器模式
            "ajax": {
            	url: datalist_url,
            },
            "autoWidth": false,
            "columns": [
                {
                    "data": "avatarUrl",
                    "render": function(data, type, row, meta) {
                        if(data){
                            return '<img src="'+data+'" alt="" style="width: 40px;height: 40px;" />';
                        }else {
                            return '<img src="/public/static/admin/img/user_default.jpg" alt="" style="width: 40px;height: 40px;" />';
                        }
                    }
                },
                {"data": "nickName"},
                {"data": "mobile"},
                {"data": "memType"},
                {
                    "data": "create_time",
                    "render": function(data, type, row, meta) {
                        return moment(data).format('YYYY-MM-DD HH:mm');
                    }
                },
                {"data": "consumeCount"},
                {
                    "data": "consumeAmount",
                    "render": function(data, type, row, meta) {
                        return data;
                        //return data / 100;
                    }
                },
                {
                    "data": "balance",
                    "render": function(data, type, row, meta) {
                        return data / 100;
                    }
                },
                {"width": "10%", "data": "null", "defaultContent": '<button type="button" class="btn btn-info view-btn">详情</button>'}
            ],
            "language": {
            	url: '/public/static/assets/pages/Chinese.json'
            },
            "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
            "pageLength": 10,
            "scrollX":"",
            "destroy": false,
            "stateSave": true,
            "stateSaveParams": function (settings, data) {
                var search = data.search.search;
                if(search){
                    var arr = $.parseJSON(search);
                    for(var key in arr){
                        $("#searchForm input[name='"+key+"']").val(arr[key]);
                        $("#searchForm select[name='"+key+"']").val(arr[key]);
                    }
                }
            },
            "order": [
                [4, "desc"]
            ],
            "columnDefs": [{
                "orderable": false,
                "targets": [0, 2, 3, 5, 6, 7]
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete": function() {
            	var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            }
        });

        // 商品修改
        table.on('click', '.view-btn', function(event) {
            var oData_arr = table.DataTable().rows($(this).parents("tr")).data(); // 操作行对象
            var id = oData_arr[0].id;

            window.location.href = view_url + "?id="+encodeURIComponent(id);
        });
    };

    return {
        init: function() {
            _init();
        }
    };

}();


$(function() {
    Member.init();

    $('#doSearch').on('click', function(event){  //商品搜索
        var typeId = $("#search select[name='typeId']").val();
        var mobile = $("#search input[name='mobile']").val();
        var create_time = $("#search input[name='create_time']").val();
        //console.log(create_time);
        var param = {
            "typeId" : typeId,
            "mobile" : mobile,
            "create_time" : create_time
        };

        $('#data-table').DataTable().search(JSON.stringify(param)).draw();
    });

    // 模板下载
    $('#download_template').on('click', function(){
        //
    });

    // 批量导入
    $('#import_csv').on('click', function(){
        var csv_file = $('input[name=csv_file]').val();
        var point = csv_file.lastIndexOf(".");

        var type = csv_file.substr(point);
        type = type.toLowerCase();
        if(type != ".csv" && type != ".xls" && type != ".xlsx"){
            layer.msg('请选择csv, xls, xlsx文件');
            return false;
        }

        //$('#import_form').submit();
        var url = $('#import_form').attr('action');
        //var data = $('#import_form').serialize();
        //var csv_file = $('#import_form').find('input[name=csv_file]').val();
        //var data = {'csv_file':csv_file};
        //var data = new FormData($('#import_form'));
        var data = new FormData();
        var csv_file = document.getElementById('csv_file').files[0];
        data.append('csv_file', csv_file);
        //console.log(data);

        layer.load(2, {shade: [0.8,'#000000']}); // 打开loading
        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            //dataType : 'json',
            processData: false,
            contentType: false,
            success: function (data) {
                layer.closeAll('loading'); // 关闭loading
                layer.msg(data.msg);
                if(data.status == 1){
                    window.location.reload();
                }
            }
        });
        return false;
    });

    // 批量导出
    $('#export_csv').on('click', function(){
        var url = $(this).data('url');
        var typeId = $('select[name=typeId]').val();
        var mobile = $('input[name=mobile]').val();
        var create_time = $('input[name=create_time]').val();

        var $form = '<form id="export_form" action="'+url+'" method="post" style="display: none;">';
        $form += '<input name="typeId" value="'+typeId+'" >';
        $form += '<input name="mobile" value="'+mobile+'" >';
        $form += '<input name="create_time" value="'+create_time+'" >';
        $form += '</form>';
        if($(document).find('#export_form').length){
            $(document).find('#export_form').remove();
        }
        $(document.body).append($form);
        // 提交表单，实现下载
        $(document).find('#export_form').submit();
    });

});
